<template>
  <el-container class="home_container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="login_out">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isColzlapse ? '64px' : '200px'">
        <!-- 菜单栏折叠按钮 -->
        <div class="togle_button" @click="change">|||</div>
        <!-- 左侧菜单 -->
        <el-menu
        unique-opened
        router
        :collapse="isColzlapse"
        :collapse-transition="false"
        :default-active="activePath"
        background-color="#333744"
        text-color="#fff"
        active-text-color="#409eff">
          <!-- 一级菜单 -->
          <el-submenu :index="'/'  + item.path" v-for="item in menusList" :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
              <el-menu-item :index="'/'  + item2.path" v-for="item2 in item.children" :key="item2.id" @click="saveActive(item2.path)">
                <!-- 二级菜单模板 -->
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ item2.authName }}</span>
                </template>
              </el-menu-item>
          </el-submenu>

    </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
</el-container>
</template>

<script>
export default {
  data () {
    return {
      // 菜单列表
      menusList: [],
      // 动态渲染icon
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 左侧菜单是否折叠
      isColzlapse: false,
      // 保存被选中的菜单项
      activePath: window.sessionStorage.getItem('active')
    }
  },
  created () {
    this.getMenuList()
  },
  methods: {
    login_out () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('获取菜单列表失败')
      this.menusList = res.data
    },
    change () {
      this.isColzlapse = !this.isColzlapse
    },
    saveActive (path) {
      window.sessionStorage.setItem('active', '/' + path)
    }
  }
}
</script>

<style scoped lang="less">
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  div {
    display: flex;
    align-items: center;
    span {
      margin-left: 20px;
      color: #fff;
      font-size: 20px;
    }
  }
}
.el-aside {
  background-color: #333744;
}
.home_container {
  height: 100%;
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border: none;
}
.togle_button {
  background-color: #4a5064;
  font-size: 10px;
  color: #fff;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
